<?php
class Order {
	private $registry;
	private $status;
	
	public function __construct($registry) {
		$this->registry = $registry;
		// 订单状态：0.等待支付，1.等待确认支付，2.支付异常，3.支付完成，4.订单完成，5.超时关闭，6.订单取消
		$this->status = array(
			0 => array(0),
			1 => array(0, 1),
			2 => array(0, 1),
			3 => array(0, 1, 2),
			4 => array(0, 1, 2, 3),
			5 => array(0, 1, 2),
			6 => array(0, 1, 2, 3)
		);
	}

	public function __get($key) {
		return $this->registry->get($key);
	}
	
	public function __set($key, $value) {
		$this->registry->set($key, $value);
	}
	/**
	 * 订单更新总入口
	 * 
	 * @param array $data(
	 * int * id: 订单号,
	 * int * status: 新状态,
	 * int paymented: 已支付金额,
	 * string sysRemark: 系统备注
	 * )
	 * @param unknown_type $extend: 扩展参数
	 * @return boolean
	 */
	public function update($data, $extend = 0) {
		// 是否合法状态值
		if ( !array_key_exists($data['status'], $this->status) ) {
			return false;
		}
		// 读取订单信息
		$this->load->model('order/order');
		$order_data = $this->model_order_order->get($data['id']);
		if ( empty($order_data) ) {
			return false;
		}
		// 是否合法状态转变
		if ( !in_array($order_data['status'], $this->status[$data['status']]) ) {
			return false;
		}
		
		$result = false;
		
		switch ( $order_data['type'] ) {
			case CONF_ORDER_SPORT :
				$result = $this->updateSport($data['id'], $data['status'], $extend);
				break;
		}
		if ( $result ) {
			// 更新全局订单
			$_data = array(
				'id' => $data['id'],
				'status' => $data['status']
			);
			if ( isset($data['paymented']) ) {
				$_data['paymented'] = $data['paymented'];
			}
			if ( isset($data['sysRemark']) ) {
				$_data['sysRemark'] = $data['sysRemark'];
			}
			//print_r($_data);
			$this->model_order_order->update($_data);
			
			return true;
		}
		else {
			return false;
		}
	}
	/**
	 * 运动订单状态更新
	 * 
	 * @param int $id: 订单号
	 * @param int $status: 订单状态
	 * @param int $extend: 扩展参数[0.线上订单, 1.线下订单]
	 * 
	 * @author zhuzhenguo
	 * Modified 2013/01/08 19:48
	 * 1.初创
	 */
	private function updateSport($id, $status, $extend = 0) {
		$this->load->model('sport/order');
		$this->load->model('sport/plan');
		
		$_order_data = $this->model_sport_order->get($id);
		if ( empty($_order_data) ) {
			return false;
		}
		
		// 订单子项信息
		$_order_item = $this->model_sport_order->getItemList( array('orderID' => $id) );
		// 获取plan ID列表
		$_plan_id_list = array();
		foreach ($_order_item as $item) {
			$_plan_id_list[] = $item['planId'];
		}
		
		$_data = array();
		switch ( $status ) {
			case 0: 
				$_data = array(
					'idList' => $_plan_id_list,
					'status' => $extend == 0 ? 5 : 3, // 订单状态改为[等待支付]，plan线上订单plan状态为5[线上锁定]，线下订单为3[预定暂锁]
					'lockedOrderId' => $id
				);
				break;
			case 1:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => $extend == 0 ? 5 : 4, // 订单状态改为[等待确认支付]，plan线上订单plan状态依然为5[线上锁定]，线下订单为4[线下未支付]
						'lockedOrderId' => $id
				);
				break;
			case 2:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => $extend == 0 ? 5 : 4, // 订单状态改为[支付异常]，plan线上订单plan状态依然为5[线上锁定]，线下订单依然为4[线下未支付]
						'lockedOrderId' => $id
				);
				break;
			case 3:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => $extend == 0 ? 2 : 1, // 订单状态改为[支付完成]，plan线上订单plan状态为2[线上已售]，线下订单为1[线下已售]
						'lockedOrderId' => $id
				);
				if ( $extend == 0 ) {
					// 随机验证码
					$_rndCode = getRandom( array('number' => true, 'lcase' => 0, 'ucase' => 0, 'length' => 8) );
					// 订单信息
					$_orderInfo = array();
					foreach ($_order_item as $item) {
						$_oderItemTime = strtotime($item['endTime']) - strtotime($item['startTime']) > 3600 ? date('H:i', strtotime($item['startTime'])) . '-' . date('H:i', strtotime($item['endTime'])) : date('H:i', strtotime($item['startTime']));
						$_orderInfo[$item['dueDate']][] = $_oderItemTime . ' ' . $item['spaceName'];
					}
					$_orderInfoText = $_order_data['venueName'] . "：\n";
					foreach ($_orderInfo as $key => $item) {
						$_orderInfoText .= "<" . $key . ">\n" . implode("；\n", $item) . "。\n";
					}
					$this->load->library('sms');
					$sms = new Sms($this->registry);
					$_smsData = array(
						'__orderId__' => $id,
						'__code__' => $_rndCode,
						'__orderData__' => $_orderInfoText
					);
					$sms->sendSms($_order_data['telephone'], 'sportOrderCompletePayment', $_smsData);
				}
				break;
			case 4:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => $extend == 0 ? 2 : 1, // 订单状态改为[订单完成]，plan线上订单plan状态依然为2[线上已售]，线下订单依然为1[线下已售]
						'lockedOrderId' => $id
				);
				break;
			case 5:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => 0, // 订单状态改为[超时关闭]，plan状态为0[空闲]
						'lockedOrderId' => $id
				);
				break;
			case 6:
				$_data = array(
						'idList' => $_plan_id_list,
						'status' => 0, // 订单状态改为订单取消，plan状态为0[空闲]
						'lockedOrderId' => $id
				);
				break;
		}
		// 更新plan
		$this->model_sport_plan->updatePlan( $_data );
		// 更新运动订单
		$_data = array(
			'status' => $status
		);
		if ( isset($_rndCode) ) {
			$data['pwd'] = $_rndCode;
		}
		$this->model_sport_order->editOrder($id, $_data);
		
		return true;
	}
}
?>